home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / delphi2 / delphite.exe / data.z / GRIDS.INT < prev    next >
Encoding:
Text File  |  1996-08-12  |  17.7 KB  |  452 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 1995,96 Borland International     }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit Grids;
  11.  
  12. {$R-}
  13.  
  14. interface
  15.  
  16. uses SysUtils, Messages, Windows, Classes, Graphics, Menus, Controls, Forms,
  17.   StdCtrls, Mask;
  18.  
  19. const
  20.   MaxCustomExtents = MaxListSize;
  21.   MaxShortInt = High(ShortInt);
  22.  
  23. type
  24.   EInvalidGridOperation = class(Exception);
  25.  
  26.   { Internal grid types }
  27.   TGetExtentsFunc = function(Index: Longint): Integer of object;
  28.  
  29.   TGridAxisDrawInfo = record
  30.     EffectiveLineWidth: Integer;
  31.     FixedBoundary: Integer;
  32.     GridBoundary: Integer;
  33.     GridExtent: Integer;
  34.     LastFullVisibleCell: Longint;
  35.     FullVisBoundary: Integer;
  36.     FixedCellCount: Integer;
  37.     FirstGridCell: Integer;
  38.     GridCellCount: Integer;
  39.     GetExtent: TGetExtentsFunc;
  40.   end;
  41.  
  42.   TGridDrawInfo = record
  43.     Horz, Vert: TGridAxisDrawInfo;
  44.   end;
  45.  
  46.   TGridState = (gsNormal, gsSelecting, gsRowSizing, gsColSizing,
  47.     gsRowMoving, gsColMoving);
  48.  
  49.   { TInplaceEdit }
  50.   { The inplace editor is not intended to be used outside the grid }
  51.  
  52.   TCustomGrid = class;
  53.  
  54.   TInplaceEdit = class(TCustomMaskEdit)
  55.   protected
  56.     procedure CreateParams(var Params: TCreateParams); override;
  57.     procedure DblClick; override;
  58.     function EditCanModify: Boolean; override;
  59.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  60.     procedure KeyPress(var Key: Char); override;
  61.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  62.     procedure BoundsChanged; virtual;
  63.     procedure UpdateContents; virtual;
  64.     procedure WndProc(var Message: TMessage); override;
  65.     property  Grid: TCustomGrid;
  66.   public
  67.     constructor Create(AOwner: TComponent); override;
  68.     procedure Deselect;
  69.     procedure Hide;
  70.     procedure Invalidate;
  71.     procedure Move(const Loc: TRect);
  72.     function PosEqual(const Rect: TRect): Boolean;
  73.     procedure SetFocus;
  74.     procedure UpdateLoc(const Loc: TRect);
  75.     function Visible: Boolean;
  76.   end;
  77.  
  78.   { TCustomGrid }
  79.  
  80.   { TCustomGrid is an abstract base class that can be used to implement
  81.     general purpose grid style controls.  The control will call DrawCell for
  82.     each of the cells allowing the derived class to fill in the contents of
  83.     the cell.  The base class handles scrolling, selection, cursor keys, and
  84.     scrollbars.
  85.       DrawCell
  86.         Called by Paint. If DefaultDrawing is true the font and brush are
  87.         intialized to the control font and cell color.  The cell is prepainted
  88.         in the cell color and a focus rect is drawn in the focused cell after
  89.         DrawCell returns.  The state passed will reflect whether the cell is
  90.         a fixed cell, the focused cell or in the selection.
  91.       SizeChanged
  92.         Called when the size of the grid has changed.
  93.       BorderStyle
  94.         Allows a single line border to be drawn around the control.
  95.       Col
  96.         The current column of the focused cell (runtime only).
  97.       ColCount
  98.         The number of columns in the grid.
  99.       ColWidths
  100.         The width of each column (up to a maximum MaxCustomExtents, runtime
  101.         only).
  102.       DefaultColWidth
  103.         The default column width.  Changing this value will throw away any
  104.         customization done either visually or through ColWidths.
  105.       DefaultDrawing
  106.         Indicates whether the Paint should do the drawing talked about above in
  107.         DrawCell.
  108.       DefaultRowHeight
  109.         The default row height.  Changing this value will throw away any
  110.         customization done either visually or through RowHeights.
  111.       FixedCols
  112.         The number of non-scrolling columns.  This value must be at least one
  113.         below ColCount.
  114.       FixedRows
  115.         The number of non-scrolling rows.  This value must be at least one
  116.         below RowCount.
  117.       GridLineWidth
  118.         The width of the lines drawn between the cells.
  119.       LeftCol
  120.         The index of the left most displayed column (runtime only).
  121.       Options
  122.         The following options are available:
  123.           goFixedHorzLine:     Draw horizontal grid lines in the fixed cell area.
  124.           goFixedVertLine:     Draw veritical grid lines in the fixed cell area.
  125.           goHorzLine:          Draw horizontal lines between cells.
  126.           goVertLine:          Draw vertical lines between cells.
  127.           goRangeSelect:       Allow a range of cells to be selected.
  128.           goDrawFocusSelected: Draw the focused cell in the selected color.
  129.           goRowSizing:         Allows rows to be individually resized.
  130.           goColSizing:         Allows columns to be individually resized.
  131.           goRowMoving:         Allows rows to be moved with the mouse
  132.           goColMoving:         Allows columns to be moved with the mouse.
  133.           goEditing:           Places an edit control over the focused cell.
  134.           goAlwaysShowEditor:  Always shows the editor in place instead of
  135.                                waiting for a keypress or F2 to display it.
  136.           goTabs:              Enables the tabbing between columns.
  137.           goRowSelect:         Selection and movement is done a row at a time.
  138.       Row
  139.         The row of the focused cell (runtime only).
  140.       RowCount
  141.         The number of rows in the grid.
  142.       RowHeights
  143.         The hieght of each row (up to a maximum MaxCustomExtents, runtime
  144.         only).
  145.       ScrollBars
  146.         Determines whether the control has scrollbars.
  147.       Selection
  148.         A TGridRect of the current selection.
  149.       TopLeftChanged
  150.         Called when the TopRow or LeftCol change.
  151.       TopRow
  152.         The index of the top most row displayed (runtime only)
  153.       VisibleColCount
  154.         The number of columns fully displayed.  There could be one more column
  155.         partially displayed.
  156.       VisibleRowCount
  157.         The number of rows fully displayed.  There could be one more row
  158.         partially displayed. }
  159.  
  160.   TGridOption = (goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
  161.     goRangeSelect, goDrawFocusSelected, goRowSizing, goColSizing, goRowMoving,
  162.     goColMoving, goEditing, goTabs, goRowSelect,
  163.     goAlwaysShowEditor, goThumbTracking);
  164.   TGridOptions = set of TGridOption;
  165.   TGridDrawState = set of (gdSelected, gdFocused, gdFixed);
  166.   TGridScrollDirection = set of (sdLeft, sdRight, sdUp, sdDown);
  167.  
  168.   TGridCoord = record
  169.     X: Longint;
  170.     Y: Longint;
  171.   end;
  172.  
  173.   TGridRect = record
  174.     case Integer of
  175.       0: (Left, Top, Right, Bottom: Longint);
  176.       1: (TopLeft, BottomRight: TGridCoord);
  177.   end;
  178.  
  179.   TSelectCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  180.     var CanSelect: Boolean) of object;
  181.   TDrawCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  182.     Rect: TRect; State: TGridDrawState) of object;
  183.  
  184.   TCustomGrid = class(TCustomControl)
  185.   protected
  186.     FGridState: TGridState;
  187.     FSaveCellExtents: Boolean;
  188.     function CreateEditor: TInplaceEdit; virtual;
  189.     procedure CreateParams(var Params: TCreateParams); override;
  190.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  191.     procedure KeyPress(var Key: Char); override;
  192.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  193.       X, Y: Integer); override;
  194.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  195.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  196.       X, Y: Integer); override;
  197.     procedure AdjustSize(Index, Amount: Longint; Rows: Boolean); dynamic;
  198.     function BoxRect(ALeft, ATop, ARight, ABottom: Longint): TRect;
  199.     procedure DoExit; override;
  200.     function CellRect(ACol, ARow: Longint): TRect;
  201.     function CanEditAcceptKey(Key: Char): Boolean; dynamic;
  202.     function CanGridAcceptKey(Key: Word; Shift: TShiftState): Boolean; dynamic;
  203.     function CanEditModify: Boolean; dynamic;
  204.     function CanEditShow: Boolean; virtual;
  205.     function GetEditText(ACol, ARow: Longint): string; dynamic;
  206.     procedure SetEditText(ACol, ARow: Longint; const Value: string); dynamic;
  207.     function GetEditMask(ACol, ARow: Longint): string; dynamic;
  208.     function GetEditLimit: Integer; dynamic;
  209.     function GetGridWidth: Integer;
  210.     function GetGridHeight: Integer;
  211.     procedure HideEditor;
  212.     procedure ShowEditor;
  213.     procedure ShowEditorChar(Ch: Char);
  214.     procedure InvalidateEditor;
  215.     procedure MoveColumn(FromIndex, ToIndex: Longint);
  216.     procedure ColumnMoved(FromIndex, ToIndex: Longint); dynamic;
  217.     procedure MoveRow(FromIndex, ToIndex: Longint);
  218.     procedure RowMoved(FromIndex, ToIndex: Longint); dynamic;
  219.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  220.       AState: TGridDrawState); virtual; abstract;
  221.     procedure DefineProperties(Filer: TFiler); override;
  222.     function MouseCoord(X, Y: Integer): TGridCoord;
  223.     procedure MoveColRow(ACol, ARow: Longint; MoveAnchor, Show: Boolean);
  224.     function SelectCell(ACol, ARow: Longint): Boolean; virtual;
  225.     procedure SizeChanged(OldColCount, OldRowCount: Longint); dynamic;
  226.     function Sizing(X, Y: Integer): Boolean;
  227.     procedure ScrollData(DX, DY: Integer);
  228.     procedure InvalidateCell(ACol, ARow: Longint);
  229.     procedure InvalidateCol(ACol: Longint);
  230.     procedure InvalidateRow(ARow: Longint);
  231.     procedure TopLeftChanged; dynamic;
  232.     procedure TimedScroll(Direction: TGridScrollDirection); dynamic;
  233.     procedure Paint; override;
  234.     procedure ColWidthsChanged; dynamic;
  235.     procedure RowHeightsChanged; dynamic;
  236.     procedure DeleteColumn(ACol: Longint);
  237.     procedure DeleteRow(ARow: Longint);
  238.     procedure UpdateDesigner;
  239.     property BorderStyle: TBorderStyle default bsSingle;
  240.     property Col: Longint;
  241.     property Color default clWindow;
  242.     property ColCount: Longint default 5;
  243.     property ColWidths[Index: Longint]: Integer;
  244.     property DefaultColWidth: Integer default 64;
  245.     property DefaultDrawing: Boolean default True;
  246.     property DefaultRowHeight: Integer default 24;
  247.     property EditorMode: Boolean;
  248.     property FixedColor: TColor default clBtnFace;
  249.     property FixedCols: Integer default 1;
  250.     property FixedRows: Integer default 1;
  251.     property GridHeight: Integer;
  252.     property GridLineWidth: Integer default 1;
  253.     property GridWidth: Integer;
  254.     property InplaceEditor: TInplaceEdit;
  255.     property LeftCol: Longint;
  256.     property Options: TGridOptions default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect];
  257.     property ParentColor default False;
  258.     property Row: Longint;
  259.     property RowCount: Longint default 5;
  260.     property RowHeights[Index: Longint]: Integer;
  261.     property ScrollBars: TScrollStyle default ssBoth;
  262.     property Selection: TGridRect;
  263.     property TabStops[Index: Longint]: Boolean;
  264.     property TopRow: Longint;
  265.     property VisibleColCount: Integer;
  266.     property VisibleRowCount: Integer;
  267.   public
  268.     constructor Create(AOwner: TComponent); override;
  269.     destructor Destroy; override;
  270.   published
  271.     property TabStop default True;
  272.   end;
  273.  
  274.   { TDrawGrid }
  275.  
  276.   { A grid relies on the OnDrawCell event to display the cells.
  277.      CellRect
  278.        This method returns control relative screen coordinates of the cell or
  279.        an empty rectangle if the cell is not visible.
  280.      EditorMode
  281.        Setting to true shows the editor, as if the F2 key was pressed, when
  282.        goEditing is turned on and goAlwaysShowEditor is turned off.
  283.      MouseToCell
  284.        Takes control relative screen X, Y location and fills in the column and
  285.        row that contain that point.
  286.      OnColumnMoved
  287.        Called when the user request to move a column with the mouse when
  288.        the goColMoving option is on.
  289.      OnDrawCell
  290.        This event is passed the same information as the DrawCell method
  291.        discussed above.
  292.      OnGetEditMask
  293.        Called to retrieve edit mask in the inplace editor when goEditing is
  294.        turned on.
  295.      OnGetEditText
  296.        Called to retrieve text to edit when goEditing is turned on.
  297.      OnRowMoved
  298.        Called when the user request to move a row with the mouse when
  299.        the goRowMoving option is on.
  300.      OnSetEditText
  301.        Called when goEditing is turned on to reflect changes to the text
  302.        made by the editor.
  303.      OnTopLeftChanged
  304.        Invoked when TopRow or LeftCol change. }
  305.  
  306.   TGetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; var Value: string) of object;
  307.   TSetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; const Value: string) of object;
  308.   TMovedEvent = procedure (Sender: TObject; FromIndex, ToIndex: Longint) of object;
  309.  
  310.   TDrawGrid = class(TCustomGrid)
  311.   protected
  312.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  313.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  314.       AState: TGridDrawState); override;
  315.     function GetEditMask(ACol, ARow: Longint): string; override;
  316.     function GetEditText(ACol, ARow: Longint): string; override;
  317.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  318.     function SelectCell(ACol, ARow: Longint): Boolean; override;
  319.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  320.     procedure TopLeftChanged; override;
  321.   public
  322.     function CellRect(ACol, ARow: Longint): TRect;
  323.     procedure MouseToCell(X, Y: Integer; var ACol, ARow: Longint);
  324.     property Canvas;
  325.     property Col;
  326.     property ColWidths;
  327.     property EditorMode;
  328.     property GridHeight;
  329.     property GridWidth;
  330.     property LeftCol;
  331.     property Selection;
  332.     property Row;
  333.     property RowHeights;
  334.     property TabStops;
  335.     property TopRow;
  336.   published
  337.     property Align;
  338.     property BorderStyle;
  339.     property Color;
  340.     property ColCount;
  341.     property Ctl3D;
  342.     property DefaultColWidth;
  343.     property DefaultRowHeight;
  344.     property DefaultDrawing;
  345.     property DragCursor;
  346.     property DragMode;
  347.     property Enabled;
  348.     property FixedColor;
  349.     property FixedCols;
  350.     property RowCount;
  351.     property FixedRows;
  352.     property Font;
  353.     property GridLineWidth;
  354.     property Options;
  355.     property ParentColor;
  356.     property ParentCtl3D;
  357.     property ParentFont;
  358.     property ParentShowHint;
  359.     property PopupMenu;
  360.     property ScrollBars;
  361.     property ShowHint;
  362.     property TabOrder;
  363.     property TabStop;
  364.     property Visible;
  365.     property VisibleColCount;
  366.     property VisibleRowCount;
  367.     property OnClick;
  368.     property OnColumnMoved: TMovedEvent;
  369.     property OnDblClick;
  370.     property OnDragDrop;
  371.     property OnDragOver;
  372.     property OnDrawCell: TDrawCellEvent;
  373.     property OnEndDrag;
  374.     property OnEnter;
  375.     property OnExit;
  376.     property OnGetEditMask: TGetEditEvent;
  377.     property OnGetEditText: TGetEditEvent;
  378.     property OnKeyDown;
  379.     property OnKeyPress;
  380.     property OnKeyUp;
  381.     property OnMouseDown;
  382.     property OnMouseMove;
  383.     property OnMouseUp;
  384.     property OnRowMoved: TMovedEvent;
  385.     property OnSelectCell: TSelectCellEvent;
  386.     property OnSetEditText: TSetEditEvent;
  387.     property OnStartDrag;
  388.     property OnTopLeftChanged: TNotifyEvent;
  389.   end;
  390.  
  391.   { TStringGrid }
  392.  
  393.   { TStringGrid adds to TDrawGrid the ability to save a string and associated
  394.     object (much like TListBox).  It also adds to the DefaultDrawing the drawing
  395.     of the string associated with the current cell.
  396.       Cells
  397.         A ColCount by RowCount array of strings which are associated with each
  398.         cell.  By default, the string is drawn into the cell before OnDrawCell
  399.         is called.  This can be turned off (along with all the other default
  400.         drawing) by setting DefaultDrawing to false.
  401.       Cols
  402.         A TStrings object that contains the strings and objects in the column
  403.         indicated by Index.  The TStrings will always have a count of RowCount.
  404.         If a another TStrings is assigned to it, the strings and objects beyond
  405.         RowCount are ignored.
  406.       Objects
  407.         A ColCount by Rowcount array of TObject's associated with each cell.
  408.         Object put into this array will *not* be destroyed automatically when
  409.         the grid is destroyed.
  410.       Rows
  411.         A TStrings object that contains the strings and objects in the row
  412.         indicated by Index.  The TStrings will always have a count of ColCount.
  413.         If a another TStrings is assigned to it, the strings and objects beyond
  414.         ColCount are ignored. }
  415.  
  416.   TStringGrid = class;
  417.  
  418.   TStringGridStrings = class(TStrings)
  419.   protected
  420.     procedure Clear; override;
  421.     function Add(const S: string): Integer; override;
  422.     function Get(Index: Integer): string; override;
  423.     function GetCount: Integer; override;
  424.     function GetObject(Index: Integer): TObject; override;
  425.     procedure Put(Index: Integer; const S: string); override;
  426.     procedure PutObject(Index: Integer; AObject: TObject); override;
  427.     procedure SetUpdateState(Updating: Boolean); override;
  428.   public
  429.     constructor Create(AGrid: TStringGrid; AIndex: Longint);
  430.     procedure Assign(Source: TPersistent); override;
  431.   end;
  432.  
  433.  
  434.   TStringGrid = class(TDrawGrid)
  435.   protected
  436.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  437.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  438.       AState: TGridDrawState); override;
  439.     function GetEditText(ACol, ARow: Longint): string; override;
  440.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  441.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  442.   public
  443.     constructor Create(AOwner: TComponent); override;
  444.     destructor Destroy; override;
  445.     property Cells[ACol, ARow: Integer]: string;
  446.     property Cols[Index: Integer]: TStrings;
  447.     property Objects[ACol, ARow: Integer]: TObject;
  448.     property Rows[Index: Integer]: TStrings;
  449.   end;
  450.  
  451. implementation
  452.